package com.chuangfeng.building.ms.login.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.chuangfeng.building.common.CoreAction;
import com.chuangfeng.building.entity.TAdminInfo;
import com.chuangfeng.building.ms.login.service.AdminMng;
import com.chuangfeng.building.ms.menu.service.MsMenuMng;
import com.chuangfeng.building.ms.menu.vo.TPCMenu;
import com.chuangfeng.framework.core.common.config.Constants;

@SuppressWarnings("serial")
@Scope("prototype")
@Controller("ms.login")
public class LoginAction extends CoreAction<TAdminInfo,AdminMng>{
	
	private final Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private AdminMng adminMngImpl;

	@Autowired
	private MsMenuMng msMenuMng;
	
	public String doLogin(){
		logger.debug("用户登入");
		try{
			String userName = request.getParameter("userName");
			String password = request.getParameter("password");
			TAdminInfo adminInfo = adminMngImpl.checkAdmin(userName, password);
			if(adminInfo==null){
				return "login";
			}else{
				session.setAttribute(Constants.SESSION_ADMIN, adminInfo);
				return "main";
			}
		}catch(Exception e){
			logger.error(e.getMessage());
			return "error";//返回错误页面
		}
	}
	
	/**
	 * 根据角色ID 得到菜单列表
	 * @return
	 */
	public String getMenu(){
		logger.debug("得到用户左边菜单");
		try{
			TAdminInfo adminInfo = (TAdminInfo)session.getAttribute(Constants.SESSION_ADMIN);
			Integer roleId = 1;
			//if(session.getAttribute("menuList")==null){
				List<TPCMenu> menuList = msMenuMng.getMenuListByRoleId(roleId);
				session.setAttribute("menuList", menuList);
			//}		
			return "leftPage";
		}catch(Exception e){
			logger.error(e.getMessage());
			return "error";//返回错误页面
		}
	}
}
